from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.generics import ListAPIView, ListCreateAPIView
from apps.users.models import User
from apps.meiduo_admin.utils.utils import UsersSerializers
from apps.meiduo_admin.utils.pager import MyUserPage
# 获取查询用户
class GetCleckUserAll(ListCreateAPIView):
    queryset = User.objects.all()  # 视图的查询集
    serializer_class = UsersSerializers  # 指定序列
    pagination_class = MyUserPage  # 在视图中指定分页器

    # def get(self, request):
    #     '''返回用户数据'''
    #     return self.list(request)
    # def create(self, request, *args, **kwargs):
    #     # 拿到数据
    #     data = request.data
    #     username = data.get('username')
    #     password = data.get('password')
    #     mobile = data.get('mobile')
    #     email = data.get('email')
    #
    #     try:
    #         user = User.objects.create_user(username=username, password=password,
    #                                         mobile=mobile, email=email)
    #     except User.DoesNotExist as e:
    #         return Response({'code': 400, 'errmsg': '用户保存失败'})
    #     else:
    #         return user




    def get_queryset(self):
        # 拿到keyword
        keyword = self.request.query_params.get('keyword')

        if not keyword:
            # 说明查询所有的数据
            return User.objects.all()
        else:
            return User.objects.filter(username__contains=keyword)